package cn.com.code.generator.service.impl;

import cn.com.code.generator.dao.ProcessStatisticDao;
import cn.com.code.generator.entity.ProcessStatistic;
import cn.com.code.generator.service.ProcessStatisticService;
import cn.com.do1.dsf.compont.mysql.utils.PageUtils;
import cn.com.do1.dsf.compont.mysql.utils.Query;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("processStatisticService")
public class ProcessStatisticServiceImpl extends ServiceImpl<ProcessStatisticDao, ProcessStatistic> implements ProcessStatisticService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        Page<ProcessStatistic> page = this.selectPage(
                new Query<ProcessStatistic>(params).getPage(),
                getEntityWrapper(params)
        );

        return new PageUtils(page);
    }

    @Override
    public List<ProcessStatistic> queryAll(Map<String, Object> params) {
        return this.selectList(getEntityWrapper(params));
    }

    private Wrapper<ProcessStatistic> getEntityWrapper(Map<String, Object> params){
                                                        		String appName = (String)params.get("appName");
                                                		String deptName = (String)params.get("deptName");
                                                		String assigneeName = (String)params.get("assigneeName");
                                                		String processDefinitionName = (String)params.get("processDefinitionName");
                                                                		String taskName = (String)params.get("taskName");
                                		Date startTime = (Date)params.get("startTime");
                                		Date endTime = (Date)params.get("endTime");
                                		Long duration = (Long)params.get("duration");
                                		String appKey = (String)params.get("appKey");
                                		Integer annual = (Integer)params.get("annual");
                
        return new EntityWrapper<ProcessStatistic>()
                                                        				.like(StringUtils.isNotEmpty(appName),"app_name", appName)
                                                				.like(StringUtils.isNotEmpty(deptName),"dept_name", deptName)
                                                				.like(StringUtils.isNotEmpty(assigneeName),"assignee_name", assigneeName)
                                                				.like(StringUtils.isNotEmpty(processDefinitionName),"process_definition_name", processDefinitionName)
                                                                				.like(StringUtils.isNotEmpty(taskName),"task_name", taskName)
                                				.eq(startTime != null,"start_time", startTime)
                                				.eq(endTime != null,"end_time", endTime)
                                				.eq(duration != null,"duration", duration)
                                				.like(StringUtils.isNotEmpty(appKey),"app_key", appKey)
                                				.eq(annual != null,"annual", annual)
                        ;
    }

}
